package com.edu.kindergarten.ui;

import java.util.ArrayList;

import android.R.integer;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.PopupWindow.OnDismissListener;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TextView;

import com.edu.kindergarten.R;
import com.edu.kindergarten.config.Constants;
import com.edu.kindergarten.server.AccountHelper;
import com.edu.kindergarten.utils.DensityUtils;
import com.edu.kindergarten.utils.LogUtils;
import com.edu.kindergarten.utils.MyUtils;
import com.edu.kindergarten.utils.ScreenUtils;
import com.edu.kindergarten.utils.UiUtils;

public class GrowthRecordActivity extends BaseFragmentActivity {
	
	private static final String TAG = "GrowthRecordActivity";
	
	private static final int GET_IMAGE_ACTIVITY_REQUEST_CODE = 100;
	private static final int CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE = GET_IMAGE_ACTIVITY_REQUEST_CODE + 1;
	private static final int CAPTURE_VIDEO_ACTIVITY_REQUEST_CODE = GET_IMAGE_ACTIVITY_REQUEST_CODE + 2;
	
	private int tabIds[] = new int[]{
			R.id.tab_baby_tree,
			R.id.tab_junior_tree,
			R.id.tab_young_tree,
	};
	
	private GrowthTreeFragment mBabyTreeFragment;
	private GrowthTreeFragment mJuniorTreeFragment;
	private GrowthTreeFragment mYoungTreeFragment;
	private ArrayList<RadioButton> buttons = new ArrayList<RadioButton>();
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		LogUtils.d(TAG,"onCreate");
		setContentView(R.layout.layout_growth_record_activity);
		
		//take photo button
		ImageView btnPhoto = (ImageView)findViewById(R.id.btn_photo);
		btnPhoto.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				addRecord();
			}
		});
		
		for (int i = 0; i < tabIds.length; i++) {
			RadioButton btn = (RadioButton)findViewById(tabIds[i]);
			buttons.add(btn);
		}
		
		//user icon and name
		ImageView userImageView = (ImageView)findViewById(R.id.user_icon);
		userImageView.setImageResource(R.drawable.logo);
		TextView nameView = (TextView)findViewById(R.id.name);
		nameView.setText(AccountHelper.getLastLoginUserName(this));
		
		//tab
		RadioGroup tabButtonGroup = (RadioGroup) findViewById(R.id.tabs);
		tabButtonGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			
			@Override
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				// TODO Auto-generated method stub
				for (int i = 0; i < tabIds.length; i++) {
					if (tabIds[i] == checkedId) {
						setSelection(i);
						setBtnColor(buttons.get(i).getId());
						break;
					}
				}
			}
		});
		
		setSelection(0);
		setBtnColor(buttons.get(0).getId());
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		LogUtils.d(TAG, "onActivityResult,requestCode:" + requestCode +
				",resultCode:" + resultCode);
		if (requestCode == GET_IMAGE_ACTIVITY_REQUEST_CODE) {
			if (resultCode == RESULT_OK) {
				/*LogUtils.d(TAG, "data uri:" + data.getData() +
						",data string:" + data.getDataString());*/
				
				Intent intent = new Intent(this, GrowthRecordEditActivity.class);
				intent.putStringArrayListExtra(Constants.INTENT_EXTRA_IMAGE_PATHS, data.getStringArrayListExtra(Constants.INTENT_EXTRA_IMAGE_PATHS));
				intent.putExtra(Constants.INTENT_EXTRA_CAPTURE_METHOD, Constants.INTENT_EXTRA_IMAGE_FROM_GALLERY);
				startActivity(intent);
			}
		}else if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) {
			if (resultCode == RESULT_OK) {
				LogUtils.d(TAG, "data uri:" + data.getData() +
						",data string:" + data.getDataString());
				
				Intent intent = new Intent(this, GrowthRecordEditActivity.class);
				intent.setData(data.getData());
				intent.putExtra(Constants.INTENT_EXTRA_CAPTURE_METHOD, Constants.INTENT_EXTRA_IMAGE_FROM_CAMERA);
				startActivity(intent);
			}
		}else if (requestCode == CAPTURE_VIDEO_ACTIVITY_REQUEST_CODE) {
			if (resultCode == RESULT_OK) {
				LogUtils.d(TAG, "data uri:" + data.getData() +
						",data string:" + data.getDataString());
				
				Intent intent = new Intent(this, GrowthRecordEditActivity.class);
				intent.setData(data.getData());
				intent.putExtra(Constants.INTENT_EXTRA_CAPTURE_METHOD, Constants.INTENT_EXTRA_VIDEO_FROM_CAMERA);
				startActivity(intent);
			}
			
		}
	}
	
	private void addRecord(){
		//UiUtils.showShortToast(this, R.string.function_not_implement);
		
		LayoutInflater inflater = LayoutInflater.from(this);
		View content = inflater.inflate(R.layout.layout_take_photo_popup, null);
		final PopupWindow window = new PopupWindow(content, 
				LayoutParams.MATCH_PARENT,
				LayoutParams.MATCH_PARENT,
				false);
		window.setOutsideTouchable(true);
		window.setFocusable(true);
		window.setOnDismissListener(new OnDismissListener() {
			
			@Override
			public void onDismiss() {
				// TODO Auto-generated method stub
				Log.d(TAG, "pop up window onDismiss");
			}
		});
		Button btnVideo = (Button)content.findViewById(R.id.video);
		btnVideo.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				takeVideo();
				window.dismiss();
			}
		});
		
		Button btnTakePhoto = (Button)content.findViewById(R.id.take_photo);
		btnTakePhoto.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				takePhotoes();
				window.dismiss();
			}
		});
		
		Button btnGetPhoto = (Button)content.findViewById(R.id.get_photo);
		btnGetPhoto.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				getPhotoes();
				window.dismiss();
			}
		});
		
		Button btnDismiss = (Button)content.findViewById(R.id.cancle);
		btnDismiss.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				window.dismiss();
			}
		});
		window.showAtLocation(getWindow().getDecorView(), Gravity.CENTER, 0, 0);
		//window.showAsDropDown(view, 0, 0);
	}
	
	private void takeVideo(){
		//UiUtils.showShortToast(this, R.string.small_video);
		Intent intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
		startActivityForResult(intent, CAPTURE_VIDEO_ACTIVITY_REQUEST_CODE);
	}
	
	private void takePhotoes(){
		//UiUtils.showShortToast(this, R.string.take_photo);
		Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
		startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
	}
	
	private void getPhotoes(){
		//UiUtils.showShortToast(this, R.string.get_photo);
		Intent intent = new Intent(this, SelectImagesActivity.class);
		startActivityForResult(intent,GET_IMAGE_ACTIVITY_REQUEST_CODE);
	}
	
	private void setBtnColor(int checkedId){
		for (int i = 0; i < buttons.size(); i++) {
			if (buttons.get(i) != null) {
				if (buttons.get(i).getId() == checkedId) {
					buttons.get(i).setTextColor(getResources().getColor(R.color.green));
				}else {
					buttons.get(i).setTextColor(getResources().getColor(R.color.black));
				}
			}
		}
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		LogUtils.d(TAG,"onDestroy");
	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		LogUtils.d(TAG,"onPause");
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		LogUtils.d(TAG,"onResume");
	}

	@Override
	protected void onStart() {
		// TODO Auto-generated method stub
		super.onStart();
		LogUtils.d(TAG,"onStart");
	}

	@Override
	protected void onStop() {
		// TODO Auto-generated method stub
		super.onStop();
		LogUtils.d(TAG,"onStop");
	}

	
	@Override
	public void onAttachFragment(Fragment fragment) {
		// TODO Auto-generated method stub
		super.onAttachFragment(fragment);
		LogUtils.d(TAG,"onAttachFragment, mBabyTreeFragment:" + mBabyTreeFragment +
				",mJuniorTreeFragment:" + mJuniorTreeFragment +
				",mYoungTreeFragment:" + mYoungTreeFragment);
		
		int type = fragment.getArguments().getInt(Constants.BUNDLE_EXTRA_GROWTH_TREE_TYPE);
		if (mBabyTreeFragment == null && type == GrowthTreeFragment.TYPE_BABY_TREE) {
			mBabyTreeFragment = (GrowthTreeFragment)fragment;
		}else if (mJuniorTreeFragment == null && type == GrowthTreeFragment.TYPE_JUNIOR_TREE) {
			mJuniorTreeFragment = (GrowthTreeFragment)fragment;
		}else if (mYoungTreeFragment == null && type == GrowthTreeFragment.TYPE_YOUNG_TREE) {
			mYoungTreeFragment = (GrowthTreeFragment)fragment;
		}
	}
	
	private void setSelection(int position){
		FragmentManager fm = getSupportFragmentManager();
		FragmentTransaction ft = fm.beginTransaction();
		hideAllFragments(ft);
		
		switch (position) {
		case 0:
			if (mBabyTreeFragment == null) {
				mBabyTreeFragment = new GrowthTreeFragment();
				Bundle bundle = new Bundle();
				bundle.putInt(Constants.BUNDLE_EXTRA_GROWTH_TREE_TYPE, GrowthTreeFragment.TYPE_BABY_TREE);
				mBabyTreeFragment.setArguments(bundle);
				ft.add(R.id.content, mBabyTreeFragment);
			}else {
				ft.show(mBabyTreeFragment);
			}
			break;
			
		case 1:
			if (mJuniorTreeFragment == null) {
				mJuniorTreeFragment = new GrowthTreeFragment();
				Bundle bundle = new Bundle();
				bundle.putInt(Constants.BUNDLE_EXTRA_GROWTH_TREE_TYPE, GrowthTreeFragment.TYPE_JUNIOR_TREE);
				mJuniorTreeFragment.setArguments(bundle);
				ft.add(R.id.content, mJuniorTreeFragment);
			}else {
				ft.show(mJuniorTreeFragment);
			}
			break;
			
		case 2:
			if (mYoungTreeFragment == null) {
				mYoungTreeFragment = new GrowthTreeFragment();
				Bundle bundle = new Bundle();
				bundle.putInt(Constants.BUNDLE_EXTRA_GROWTH_TREE_TYPE, GrowthTreeFragment.TYPE_YOUNG_TREE);
				mYoungTreeFragment.setArguments(bundle);
				ft.add(R.id.content, mYoungTreeFragment);
			}else {
				ft.show(mYoungTreeFragment);
			}
			break;

		default:
			break;
		}
		
		ft.commit();
	}
	
	private void hideAllFragments(FragmentTransaction ft){
		if (mBabyTreeFragment != null) {
			ft.hide(mBabyTreeFragment);
		}
		
		if (mJuniorTreeFragment != null) {
			ft.hide(mJuniorTreeFragment);
		}
		
		if (mYoungTreeFragment != null) {
			ft.hide(mYoungTreeFragment);
		}
	}

}